package cn.yangliu.nacos.management.repository.sys;

import java.util.List;

import cn.yangliu.jpa.base.repository.BaseRepository;
import cn.yangliu.nacos.management.entity.sys.Authority;
import org.springframework.data.jpa.repository.Query;

/**
 * The type of nacos.
 *
 * @author 问道于盲
 * @date 2020 -01-01
 */
public interface AuthorityRepository extends BaseRepository<Authority, Long> {

    /**
     * Find all authorities list.
     *
     * @return the list
     */
    @Query("from Authority where button = true")
    List<Authority> findAllAuthorities();

    /**
     * Find authorities by oauth 2 id list.
     *
     * @param oauth2Id the oauth 2 id
     * @return the list
     */
    @Query("from Authority a " +
            "inner join Role2Authority r2a on(r2a.id.authorityId = a.id) " +
            "inner join Role r on(r.id = r2a.id.roleId) " +
            "inner join User2Role c2r on(c2r.id.roleId = r.id) " +
            "inner join User u on(u.id = c2r.id.userId) " +
            "where a.button = true and u.oauth2Id = :oauth2Id")
    List<Authority> findAuthoritiesByOauth2Id(Long oauth2Id);
}
